package top.bingoz.wanandroid.ui.activity

import android.content.Context
import android.content.Intent
import android.support.v7.widget.RecyclerView
import android.view.MotionEvent
import com.alibaba.android.vlayout.DelegateAdapter
import com.alibaba.android.vlayout.VirtualLayoutManager
import com.jakewharton.rxbinding2.view.RxView
import kotlinx.android.synthetic.main.act_main.*
import top.bingoz.wanandroid.R
import top.bingoz.wanandroid.base.BaseMvpActivity
import top.bingoz.wanandroid.base.MultiPresenter
import top.bingoz.wanandroid.bean.Banner
import top.bingoz.wanandroid.bean.Data
import top.bingoz.wanandroid.contract.ArticleContract
import top.bingoz.wanandroid.contract.BannerContract
import top.bingoz.wanandroid.logd
import top.bingoz.wanandroid.presenter.ArticlePresenter
import top.bingoz.wanandroid.presenter.BannerPresenter
import top.bingoz.wanandroid.toast
import top.bingoz.wanandroid.ui.adapter.ContentAdapter
import top.bingoz.wanandroid.ui.adapter.EmptyAdapter
import java.util.*
import javax.inject.Inject


class MainActivity : BaseMvpActivity(), ArticleContract.View, BannerContract.View {

    @Inject
    lateinit var articlePresenter: ArticlePresenter

    @Inject
    lateinit var bannerPresenter: BannerPresenter

    val mContext: Context = this


    override fun getLayoutId(): Int {
        return R.layout.act_main
    }

    override fun bindPresenter(presenter: MultiPresenter) {
        presenter.bind(articlePresenter, bannerPresenter)
    }

    override fun initialize() {

        articlePresenter.getMainArticleList(0)

        bannerPresenter.getBanner()

        RxView.clicks(layout)
                .subscribe {
                    toast("Hello, 我是后面的布局")
                }

        RxView.clicks(textView)
                .subscribe {
                    toast("我是TextView, 爪子")
                    startActivity(Intent(mContext, Images2Activity::class.java))
                }
        RxView.clicks(button)
                .subscribe {
                    toast("我是Button, 啷个")
                }

        initRecyclerView(recyclerView)


    }

    private fun initRecyclerView(view: RecyclerView) {
        val layoutManager = VirtualLayoutManager(mContext)
        // 注意：当hasConsistItemType=true的时候，不论是不是属于同一个子adapter，相同类型的item都能复用。表示它们共享一个类型
        // 当hasConsistItemType=false的时候，不同子adapter之间的类型不共享
        val delegateAdapter = DelegateAdapter(layoutManager)
        val adapters = LinkedList<DelegateAdapter.Adapter<out RecyclerView.ViewHolder>>();
        initAdapters(adapters)
        delegateAdapter.setAdapters(adapters);
        view.setLayoutManager(layoutManager);
        view.setAdapter(delegateAdapter);


    }

    private fun initAdapters(adapters: LinkedList<DelegateAdapter.Adapter<out RecyclerView.ViewHolder>>) {

        adapters.add(EmptyAdapter(mContext))
        val adapter = ContentAdapter(mContext)
        adapters.add(adapter)
        adapter.setData("Hello")

        adapter.setData(null)
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        layout.dispatchTouchEvent(ev)
        return super.dispatchTouchEvent(ev)
    }


    override fun showMainArticleList(data: Data) {
        logd(data)
    }

    override fun showBanner(banners: List<Banner>) {
        logd(banners)
    }


    override fun onBackPressed() {
        super.onBackPressed()
    }

}
